var util=require('../../util/util.js');
var app=getApp();
Page({
  data: {
   hotplay:{},
   coming:{},
   top250:{},
   search:false,
   searchresult:{}
  },
  onLoad: function (event) {
    var inTheatersUrl = app.globalData.doubanBase +
      "/v2/movie/in_theaters" + "?start=0&count=3";
    var comingSoonUrl = app.globalData.doubanBase +
      "/v2/movie/coming_soon" + "?start=0&count=3";
    var top250Url = app.globalData.doubanBase +
      "/v2/movie/top250" + "?start=0&count=3";

    wx.showNavigationBarLoading();
    console.log('show');

    this.getMovieData(inTheatersUrl, "hotplay", "正在热映");
    this.getMovieData(comingSoonUrl, "coming", "即将上映");
    this.getMovieData(top250Url, "top250", "豆瓣Top250");
  },
getMovieData:function(url,key,title){
  var self=this;
  wx.request({
    url:url,
    method:'GET',
    header:{
      "content-type":'json'
    },
    success:function(res){
      self.dealing(res.data,key,title)
    },
    fail:function(error){
      console.log(error)
    }
  })
},
dealing:function(data,key,title){
 var movies=[];
 console.log(data);
 for(var i in data.subjects){
  var subject=data.subjects[i];
  var temp={
    stars:util.convertToStarsArray(subject.rating.stars),
    title:subject.title,
    average:subject.rating.average,
    coverageUrl: subject.images.large,
    movieId: subject.id
  };
  movies.push(temp);
}
  var readyData={};
  readyData[key]={
    title:title,
    movies:movies
  };
  this.setData(readyData);
  wx.hideNavigationBarLoading();
},
tomore:function(event){
  var type=event.currentTarget.dataset.type;
  wx.navigateTo({
    url:"more-movie/more-movie?type="+type
  })
},
todetail:function(event){
 var id=event.currentTarget.dataset.id; 
 wx.navigateTo({
   url: "movie-detail/movie-detail?id=" + id
 })
},
showsearch:function(){
  this.setData({
    search:true
  })
},
hidesearch:function(){
   this.setData({
    search:false,
    searchResult: {},
    inputValue:''
  })
},
search:function(event){
  var keyword=event.detail.value;
  console.log(keyword)
   var url = app.globalData.doubanBase +
      "/v2/movie/search?q=" + keyword;
      this.getMovieData(url,'searchResult','')
  }
})